home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6603 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  98 lines

  1. Newsgroups: comp.lang.c
  2. Path: uu4news.netcom.com!zodiac!szh
  3. From: szh@zcon.com (Syed Zaeem Hosain)
  4. Subject: Re: Can someone help me with fopen
  5. Message-ID: <1996Feb15.173508.13317@zcon.com>
  6. Sender: szh@zcon.com (Syed Zaeem Hosain)
  7. Nntp-Posting-Host: zodiac
  8. Reply-To: szh@zcon.com
  9. Organization: Z Consulting Group
  10. References: <4fouoo$k0e@news.uncc.edu>
  11. Date: Thu, 15 Feb 1996 17:35:08 GMT
  12.  
  13. In article <4fouoo$k0e@news.uncc.edu>, Samuel S Feeback <ssfeebac> writes:
  14. >Here is my problem.  I am trying to read a text file, search for a certain
  15. >string and write that string to an outfile.  I am having trouble with the fopen
  16. >commnad. Here is a fragment of my code.
  17. >
  18. >char input; /* variable to put filename in*/
  19.  
  20. This is a single character. Is the file name short enough to fit
  21. in this, let alone the null byte you will need to terminate the
  22. char array? That should give you a hint about how long the variable
  23. really needs to be! (Was this a pointer, perhaps?)
  24.  
  25. >FILE *infile /* Pointer to File to be used as input */
  26. >
  27. >if (argv[argc] == "-i")
  28. >    {input = ((argv[argc]) + 1);
  29.  
  30. Yes, you got one character in the above variable "input" here. That is
  31. not going to work later.
  32.  
  33. >     infile = fopen((("%s", input), "r"));
  34.  
  35. This call is wrong. What you hand to 'fopen' is something very strange.
  36. There are way too many parentheses, and some confusion too.  Are you
  37. sure that you were not trying to 'sprintf' the variable first?  But
  38. more importantly, "input" is a single byte, so the filename is not
  39. correct anyway, most likely.
  40.  
  41. If "input" were a correct char array (or pointer pointing to a valid
  42. memory location) containing the file name, then the code for the above
  43. line would simply look like this:
  44.  
  45.     infile = fopen(input, "r");
  46.  
  47. My thought is that you are copying code from some other program (or
  48. book) without thinking too clearly about what is meant to be achieved
  49. in those lines. Try simplifying the process into simpler steps, read
  50. the manual pages on the various functions (sprintf, fopen, etc.) and
  51. then proceed.
  52.  
  53. >     if ((infile = fopen((("%s", input), "r"))) == NULL)
  54. >        {/*Error Message*/
  55.  
  56. Calling 'fopen' again, to check whether the first one returned NULL is
  57. probably an error. If the first one succeeded, then the second call is
  58. not needed.
  59.  
  60. Again, if the "infile" variable were correctly specified (as I mention
  61. above), then the following would be better code:
  62.  
  63.     infile = fopen(input, "r");
  64.     if (infile == NULL)
  65.         {
  66.  
  67. An alternative is to combine the two above, but here is where you have
  68. to be careful to use the right parentheses to ensure that the fopen
  69. returned value assignment is done before the comparison (I have put
  70. dashes and bars to show the parenthesis matching - this ain't part of
  71. the code!):
  72.  
  73.             ------------------------------------
  74.            | --------------------------         |
  75.            ||               ---------- |        |
  76.            ||              |          ||        |
  77.         if ((infile = fopen(input, "r")) == NULL)
  78.         {
  79.  
  80. >I keep getting an "too few arguments to function `fopen'
  81.  
  82. Yes. That is the difficulty - or at least one of them!
  83.  
  84. >Please e-mail me is you have any idea on this problem(or any other aspect of
  85. >    this program for that matter :>)
  86.  
  87. We cannot. Your e-mail address in this post is faulty. Please see the
  88. reference above (beginning with "In article ...").
  89.  
  90.                                 Z
  91.  
  92.  
  93. -- 
  94. -------------------------------------------------------------------------
  95. | Syed Zaeem Hosain          P. O. Box 610097            (408) 441-7021 |
  96. | Z Consulting Group        San Jose, CA 95161             szh@zcon.com |
  97. -------------------------------------------------------------------------
  98.